package org.ayachinene.leetcode.problems;

public class P1663 {

    static class Solution {
        public String getSmallestString(int n, int k) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; ++i) {
                // 还剩下几位
                int leftN = n - i - 1;
                char c;
                if (leftN * 26 >= k) {
                    c = 'a';
                } else {
                    c = getChar(k - leftN * 26);
                }
                sb.append(c);
                k -= getValue(c);
            }
            return sb.toString();
        }

        // 返回数值val对应的字符
        // 1: 'a'
        // 2: 'b'
        // ...
        private char getChar(int val) {
            return (char) ('a' + val - 1);
        }

        // 返回c的数值
        // 'a': 1
        // 'b': 2
        // ...
        private int getValue(char c) {
            return c - 'a' + 1;
        }
    }
}
